﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileManager;

using System.Web;
using FileManager.Toolkit;
using System.IO;
using System.Drawing;

namespace DalatCooking.Core.Web.HttpHandlers
{
    public class ImageUploadBase
    {
        public static bool ImageUpload(HttpPostedFile postedFile, ref string errMess, ref string path)
        {
            string _uploadFolder = Setting.String("FILE_IMAGEFOLDER");

            string _imageResizedPath = Setting.String("FILE_IMAGEFOLDER");
           
            UploadHandler upload = new UploadHandler();

            upload.AllowedExtensions = Setting.String("FILE_ALLOW_IMAGEEXT");
            upload.GenerateUniqueFileName = Setting.Boolean("FILE_ALLOW_UNIQUENAME");

            upload.VirtualSavePath = _uploadFolder;
            upload.FileContentLenght = Setting.Int("FILE_MAXSIZE");
            upload.OverwriteExistingFile = Setting.Boolean("FILE_ALLOW_OVERWRITE");
            upload.GenerateDateFolder = Setting.Boolean("FILE_ALLOW_DATEFOLDER");
            upload.AddTextOnImage = Setting.Boolean("IMAGE_ALLOW_ADDTEXT");
            upload.ResizeImage = Setting.Boolean("IMAGE_ALLOW_RESIZE");

            int imageWidth = Setting.Int("IMAGE_WIDTH");
            int imageHeight = Setting.Int("IMAGE_HEIGHT"); 
            try
            {
                
                // Upload Images
                if (upload.UploadFile(postedFile, ref errMess, ref path) == true)
                {

                    // Copy file to Resize
                    // FileHelper.CopyFile(context.Server.MapPath(path), context.Server.MapPath(_imageResizedPath));
                    // Crop Image
                    // string fileToResize = context.Server.MapPath(_imageResizedPath + "/" + errMess);
                    // Imaging.ResizeImage(fileToResize, 105);
                    // Imaging.ResizeImage(fileToResize, fileToResize, 105, System.Drawing.Imaging.ImageFormat.Png);
                    // string fileResized = _imageResizedPath + "/" + errMess;
                    if (!path.Contains(".swf"))
                    {
                        if (upload.ResizeImage)
                        {
                            // Crop Image
                            string fileToResize = HttpContext.Current. Server.MapPath(path);
                            Imaging.ResizeImage(fileToResize, imageWidth, imageHeight);
                            // Imaging.ResizeImage(fileToResize, fileToResize, 105, System.Drawing.Imaging.ImageFormat.Png);

                        }
                        if (upload.AddTextOnImage)
                        {
                            string fileNameIn = HttpContext.Current.Server.MapPath(path);
                            string fileNameOut = HttpContext.Current.Server.MapPath(Path.GetDirectoryName(path) + "\\" + Guid.NewGuid() + Path.GetExtension(path));
                            string textToImage = "Le Thanh Tuan";
                            Color myColor = Color.White;
                            Point textLocation = new Point(10, 10);
                            Font f = new Font("Arial", 30f, FontStyle.Bold);
                            Imaging.AddTextToImage(fileNameIn, fileNameOut, f, myColor, textLocation, textToImage);

                            File.Delete(fileNameIn);
                            string fileNameOutput = Path.GetDirectoryName(path) + "\\" + Path.GetFileName(fileNameOut);

                            //Uri uriSiteRoot = new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority));
                            //var output = string.Format("{0}{1}", "", fileNameOutput.Replace(@"\", "/"));

                            //context.Response.ContentType = "text/plain";
                            //context.Response.Write(output);
                            path = string.Format("{0}{1}", "", fileNameOutput.Replace(@"\", "/"));
                        }
                        else
                        {
                            //Uri uriSiteRoot = new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority));
                            //var output = string.Format("{0}{1}", "", path);
                            //context.Response.ContentType = "text/plain";
                            //context.Response.Write(output);
                            path = path;
                        }
                        return true;
                    }
                    else
                    {
                       return upload.UploadFile(postedFile, ref errMess, ref path);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
